xenstore: New function xs_path_is_subpath
authorIan Jackson <ian.jackson@eu.citrix.com>
Fri, 13 Jan 2012 16:54:11 +0000 (16:54 +0000)
committerIan Jackson <ian.jackson@eu.citrix.com>
Fri, 13 Jan 2012 16:54:11 +0000 (16:54 +0000)
This utility function compares two paths, textually and reports
whether one is a subpath (a child path) of the other.

Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
Acked-by: Ian Campbell <ian.campbell@citrix.com>
Committed-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
tools/xenstore/xs.c
tools/xenstore/xs.h

index 8e54fe01c9c50e3a96dcd2279e106909d538778a..0a01675a787efae3444467df2c35bf367d74bbd8 100644 (file)
@@ -950,6 +950,23 @@ char *xs_get_domain_path(struct xs_handle *h, unsigned int domid)
        return xs_single(h, XBT_NULL, XS_GET_DOMAIN_PATH, domid_str, NULL);
 }
 
+bool xs_path_is_subpath(const char *parent, const char *child)
+{
+        size_t childlen = strlen(child);
+        size_t parentlen = strlen(parent);
+
+       if (childlen < parentlen)
+               return false;
+
+       if (memcmp(child, parent, parentlen))
+               return false;
+
+       if (childlen > parentlen && child[parentlen] != '/')
+               return false;
+
+       return true;
+}
+
 bool xs_is_domain_introduced(struct xs_handle *h, unsigned int domid)
 {
        char *domain = single_with_domid(h, XS_IS_DOMAIN_INTRODUCED, domid);
index 63f535dd553d5d3d6a7e9b08e968caf8fff57b4b..8d49e50e4636b559b60733bed516db6dca4e7905 100644 (file)
@@ -207,6 +207,13 @@ bool xs_release_domain(struct xs_handle *h, unsigned int domid);
  */
 char *xs_get_domain_path(struct xs_handle *h, unsigned int domid);
 
+/* Returns true if child is either equal to parent, or a node underneath
+ * parent; or false otherwise.  Done by string comparison, so relative and
+ * absolute pathnames never in a parent/child relationship by this
+ * definition.  Cannot fail.
+ */
+bool xs_path_is_subpath(const char *parent, const char *child);
+
 /* Return whether the domain specified has been introduced to xenstored.
  */
 bool xs_is_domain_introduced(struct xs_handle *h, unsigned int domid);